import { getDvaApp } from 'umi';

import moment from 'moment';
import { uuid } from './strUtil';

// 获取store
export function getStore() {
  return getDvaApp()._store;
}

// 获取dva数据
export function getState() {
  return getDvaApp()._store.getState();
}

// 国际化前缀函数
export function createAddPrefixFunc(prefix) {
  var func = key => prefix + key;
  return func;
}

/**
 * 跨应用路由跳转
 * @param {String} href url地址
 * @param {Object} stateObj 状态参数 可以通过history.state读取
 * @param {String} title 标题 暂时没有用
 */
export function routerGo(href = '/', title = null, stateObj = {}) {
  // window.history.pushState(stateObj, title, href);
}

export let waitTime = (time = 100) => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

// 获取顶层对象
export var getGlobal = function() {
  if (typeof self !== 'undefined') {
    return self;
  }
  if (typeof window !== 'undefined') {
    return window;
  }
  if (typeof global !== 'undefined') {
    return global;
  }
  throw new Error('unable to locate global object');
};

// 修改主题
export function setTheme(theme) {
  var store = getStore();
  store.dispatch({
    type: 'masterGlobal/setTheme',
    payload: {
      theme: theme,
    },
  });
}


// 判断是不是pc端
export function isPC() {
  var ispc=true
  var uA = navigator.userAgent.toLowerCase();
  var ipad = uA.match(/ipad/i) == "ipad";
  var iphone = uA.match(/iphone os/i) == "iphone os";
  var midp = uA.match(/midp/i) == "midp";
  var uc7 = uA.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  var uc = uA.match(/ucweb/i) == "ucweb";
  var android = uA.match(/android/i) == "android";
  var windowsce = uA.match(/windows ce/i) == "windows ce";
  var windowsmd = uA.match(/windows mobile/i) == "windows mobile"; 
  if (!(ipad || iphone || midp || uc7 || uc || android || windowsce || windowsmd)) {
      // PC 端
      ispc=true
  }else{
      ispc=false
      // 移动端
  }
  return ispc
}